package com.morehair.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.morehair.bean.bo.admin.CommentBo;
import com.morehair.bean.pojo.Comment;
import com.morehair.bean.pojo.CommentExample;
import com.morehair.bean.vo.admin.CommentListVo;
import com.morehair.bean.vo.admin.CommentVo;
import com.morehair.mapper.CommentMapper;
import com.morehair.service.admin.CommentService;
import com.morehair.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * Description:
 * Date: 2021/6/7
 * Time: 20:15
 * author: zhang wei
 */
@Service
@Transactional
public class CommentServiceImpl implements CommentService {

    @Autowired
    CommentMapper commentMapper;


    public static List<String> stringToList(String preStr) {
        List<String> list = new ArrayList<>();
        if (preStr.length() > 4) {
            String[] strArr = preStr.substring(1, preStr.length() - 1).split(", ");
            // 数组转化为List
            for (String str : strArr) {
                list.add(str.substring(1, str.length() - 1));
            }
        }
        return list;
    }


    @Override
    public CommentListVo getCommentList(Integer page, Integer limit, Integer userId, Integer valueId, String sort, String order) {
        PageHelper.startPage(page, limit);
        // GoodsExample goodsExample = new GoodsExample();
        CommentExample commentExample = new CommentExample();
        commentExample.setOrderByClause(sort + " " + order);
        CommentExample.Criteria criteria = commentExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (userId != null && !"".equals(userId)) {
            criteria.andUserIdEqualTo(userId);
        }
        if (valueId != null && !"".equals(valueId)) {
            criteria.andValueIdEqualTo(valueId);
        }

        // List<Goods> goods = goodsMapper.selectByExample(goodsExample);
        // List<Goods> goods = goodsMapper.selectByExampleWithBLOBs(goodsExample);
        List<Comment> comments = commentMapper.selectByExample(commentExample);
        List<CommentVo> commentVos = new ArrayList<>();

        for (Comment comment : comments) {
            CommentVo commentVo = new CommentVo();
            commentVo.setId(comment.getId());
            commentVo.setValueId(comment.getValueId());
            commentVo.setType(comment.getType());
            commentVo.setContent(comment.getContent());
            commentVo.setUserId(comment.getUserId());
            commentVo.setHasPicture(comment.getHasPicture());

            List<String> strings = stringToList(comment.getPicUrls());
            commentVo.setPicUrls(strings);

            commentVo.setStar(comment.getStar());
            commentVo.setAddTime(comment.getAddTime());
            commentVo.setUpdateTime(comment.getUpdateTime());
            commentVo.setDeleted(comment.getDeleted());
            commentVos.add(commentVo);
        }
        PageInfo<Comment> pageInfo = new PageInfo<>(comments);
        long total = pageInfo.getTotal();
        CommentListVo commentListVo = new CommentListVo();
        commentListVo.setTotal((int) total);
        commentListVo.setItems(commentVos);
        return commentListVo;
    }


    @Override
    public boolean deleteCommentById(CommentBo commentBo) {
        // I need updateTime = now()
        int i = commentMapper.fakeDeleteById(commentBo.getId(), DateUtil.nowTime());
        return i > 0 ? true : false;
    }

}
